package com.example.wechat;

import android.os.Bundle;
import android.view.MenuItem;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.wechat.adapter.NewsAdapter;
import com.example.wechat.model.NewsItem;
import java.util.ArrayList;
import java.util.List;

public class NewsActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private NewsAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("看一看");
        }

        initViews();
        loadNews();
    }

    private void initViews() {
        recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        
        adapter = new NewsAdapter();
        recyclerView.setAdapter(adapter);
    }

    private void loadNews() {
        List<NewsItem> newsList = new ArrayList<>();
        // 添加测试数据
        newsList.add(new NewsItem("标题1", "描述1", "https://example.com/image1.jpg", "https://example.com/news1"));
        newsList.add(new NewsItem("标题2", "描述2", "https://example.com/image2.jpg", "https://example.com/news2"));
        adapter.setNewsItems(newsList);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 